<!DOCTYPE html>
<html lang="en">

<head>
    <meta http-equiv="Content-Type" content="textml; charset=UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>登录|猫眼电影后台管理系统</title>
    <link rel="stylesheet" href="./layui/css/layui.css">
    <style>
        .login-container {
            background: gray;
            position: absolute;
            left: 0;
            top: 0;
            right: 0;
            bottom: 0;
            display: flex;
            justify-content: center;
            align-items: center;

        }

        .login-container form {
            width: 500px;
            background: #fff;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 0 5px rgba(0, 0, 0, .5);

        }

        .login-container form h1 {
            margin: 1em 0;
            text-align: center;
        }
    </style>

<body>
    <div class="login-container">
        <form class="layui-form" action="">
            <h1>欢迎使用猫眼电影后台管理系统</h1>
            <div class="layui-form-item">
                <label class="layui-form-label">账号</label>
                <div class="layui-input-block">
                    <input type="text" name="mobile" lay-verify="required" lay-vertype="tips" placeholder="请输入账号"
                        autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-block">
                    <input type="password" name="pwd" lay-verify="required" lay-vertype="tips" placeholder="请输入密码"
                        autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" type="button" lay-submit="" lay-filter="formDemo">登录</button>
                </div>
            </div>
        </form>
    </div>
    <script src="./layui/layui.js"></script>
    <script src="./js/jquery-1.12.4.min.js"></script>
    <script src="./js/js.cookie.js"></script>
    <script>
        layui.use(["form", 'layer'], function () {
            var form = layui.form;
            var layer = layui.layer;
            form.on("submit", async function (data) {
                const resp = await $.post("/api/user/login", data.field);
                if (resp) {
                    if (resp.isAdmin == false) {
                        //删除cookie
                        Cookies.remove("userId");
                        //提示错误
                        layui.layer.msg("你的账号不是管理员", {
                            icon: 2,
                            end: function () {
                                location.href = "./login.html";
                            }
                        })
                        return;
                    }
                    layer.msg("登录成功！", {
                        icon: 1,
                        time: 1000,
                        end: function () {
                            //保存用户状态
                            localStorage.setItem("name", data.field.mobile);
                            location.href = "./index.html";
                            
                        }
                    })

                } else {
                    //提示错误
                    layer.msg("账号密码错误", {
                        icon: 2
                    });
                }
            })
        })
    </script>


</body>

</html>